import pygame
import sys

class chara(object):
    def __init__(self):
        self.girlRect=pygame.Rect(0,0,32,64)
        self.allStatus = [  # 状态
            pygame.image.load('.\\spritesheets\\idle.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\run.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\jump.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\hurt.png').convert_alpha(),]
        self.status=0#默认呼吸状态
        self.girlX=2512/8/2-50
        self.girlY=304-16*13
        self.startX=0
        self.allWidth=192#默认总宽度
        self.width=32

        self.idle=True
        self.jump=False#默认自动降落
        self.jumpSpeed=0
        self.dead=False#默认活着

    def girlUpdate(self):
        if self.girlY>304 - 16 * 13:#判断是否跳了
            self.girlY = 304 - 16 * 13
            self.jump = False
            self.jumpSpeed = 0
            self.allWidth = 192
        if self.jump:
            self.allWidth=160
            self.jumpSpeed-=1
            self.girlY-=self.jumpSpeed

        # if not self.status:
        #     self.startX += 32
        #     clock.tick(6)
        #
        #     self.startX %=self.allWidth #默认宽度192 改为.width
        self.startX += 32
        clock.tick(6)
        self.startX %= self.allWidth  # 默认宽度192 改为.width
        self.girlRect[1]=self.girlY#更改y轴位置



def creatMap():
    #screen.fill((255,255,255))
    screen.blit(background, (0, 0))
    girl.status=0
    if girl.dead:
        girl.status=3
    elif girl.jump:
        girl.status=2
    screen.blit(girl.allStatus[girl.status], (girl.girlX, girl.girlY),(girl.startX,0,girl.width,64))#girl.width默认为32 大招不同
    girl.girlUpdate()
    pygame.display.update()

if __name__=='__main__':
    pygame.init()

    pygame.mixer.music.load(".\\bgm\\nature sketch.wav")  # 音乐
    pygame.mixer.music.set_volume(0.5)  # 设置音量大小0~1的浮点数
    pygame.mixer.music.play()  # 播放音频

    screen = pygame.display.set_mode((2512/6,304))
    clock = pygame.time.Clock()
    girl=chara()
    while True:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:  # 如果检测到按键按下
                if event.key == pygame.K_w and girl.girlY == 304 - 16 * 13:  # 如果键是E键
                    print("W键被按下!")
                    girl.jump=True
                    girl.jumpSpeed=15

        background=pygame.image.load('.\\magic-cliffs-PREVIEWx1.png')
        creatMap()

    pygame.quit()

